import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '../../agreement_dialog.dart';
import '../../mine/api/mine_api.dart';
import '../../mine/bean/aboutUsBean.dart';
import '../api/hxh_api.dart';
import '../bean/hxhAuditResultBean.dart';
import '../bean/hxhConfirmLoanBean.dart';
import '../bean/hxhOrderBean.dart';
import '../bean/hxhOrderStatusBean.dart';
import 'state.dart';

class HXHAuditPassLogic extends GetxController {
  final HXHAuditPassState state = HXHAuditPassState();
  List<OrderList>? orderList = [];
  @override
  void onInit() {
    super.onInit();
    getAgreement(6);
    getUserOrder();
  }
  ///订单列表
  getUserOrder() {
    HXHApi.getUserAllOrder(BaseCallback(onSuccess: (map) {
      HxhOrderBean orderBean = HxhOrderBean.fromJson(map);
      if (orderBean.data != null) {
        orderList = orderBean.data;
        if (orderList!.isNotEmpty || orderList != null) {
          state.orderNo = orderList![0].orderNo;
          state.orderYearRate = (orderList![0].orderYearRate!*100).toString();
          getAuditResult();
        } else {
          ToastUtils.showToast(orderBean.msg ?? "");
        }
      } else {
        ToastUtils.showToast(orderBean.msg ?? "");
      }
      update();
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
        update();
      });
    }));
  }

  //审核结果
  getAuditResult() {
    HXHApi.getAuditResult(
        state.orderNo!,
        BaseCallback(onSuccess: (map) {
          HxhAuditResultBean resultBean = HxhAuditResultBean.fromJson(map);
          if (resultBean.data != null) {
            // Get.back();
            state.institutionProductName =
                resultBean.data?.institutionProductName;
            state.auditTime = resultBean.data?.auditTime;
            state.status = resultBean.data?.status;
            state.auditPeriodNum = resultBean.data?.auditPeriodNum;
            state.auditAmount = resultBean.data?.auditAmount;
            update();
          } else {
            //审核失败
            Get.toNamed(BaseRouterConfig.auditRejection);
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }

//订单查看
  getOrderStatus() {
    HXHApi.getOrderStatus(
        state.orderNo!,
        BaseCallback(onSuccess: (map) {
          HxhOrderStatusBean orderStatusBean = HxhOrderStatusBean.fromJson(map);
          if (orderStatusBean.data?.status == 150 ||
              orderStatusBean.data?.status == 152) {
            confirmLoan();
          } else {

          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }

//确认借款接口
  confirmLoan() {
    HXHApi.confirmLoan(
        state.orderNo!,
        BaseCallback(onSuccess: (map) {
          HxhConfirmLoanBean hxhConfirmLoanBean =
              HxhConfirmLoanBean.fromJson(map);
          state.url = hxhConfirmLoanBean.result.url as String?;
          Get.off(WebViewCommon(
            state.url ?? '',
          ));
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
            update();
          });
        }));
  }
  void popover() {
    Future.delayed(Duration.zero, () {
      showDialog<Null>(
        context: Get.context!,
        barrierDismissible: false,
        useSafeArea: false,
        useRootNavigator: false,
        builder: (BuildContext context) {
          return AgreementDialog(
              name: '',
              content: state.agreementContent,
              submit: '我已阅读并同意授权',
              isClose: true,
              onTap: () {
                Get.back();
                confirmLoan();
              });
        },
      );
    });
  }

  //信用授权书
  getAgreement(int type) {
    MineApi.getHTML(
        type,
        BaseCallback(onSuccess: (map) {
          AboutUsBean aboutUsBean = AboutUsBean.fromJson(map);
          if (aboutUsBean.data != null) {
            state.agreementName = aboutUsBean.data?.name;
            state.agreementContent = aboutUsBean.data?.content;
          }
        }, onError: (msg) {
          Future.delayed(const Duration(milliseconds: 500), () {
            ToastUtils.showToast(msg ?? "");
          });
        }));
  }

}
